﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;
using System.IO;

namespace Business
{
    public partial class OperationLog : Form
    {
        public OperationLog()
        {
            InitializeComponent();
        }
        public MainForm m_mainform;
        private void but_query_Click(object sender, EventArgs e)
        {
            lv.Visible = true;
            lv2.Visible = false;
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("检测过程查询"))
            {
                m_mainform.baocunhxrj("检测过程查询", string.Format("检测过程查询"), "查询");// 核心功能日记  日记内容
            }
            getjilu();
        }
        ///解码
        public static string DecodeBase64(string code_type, string code)
        {
            string decode = "";
            byte[] bytes = Convert.FromBase64String(code);
            try
            {
                decode = Encoding.GetEncoding(code_type).GetString(bytes);
            }
            catch
            {
                decode = code;
            }
            return decode;
        }

        public void getjilu()
        {
            lv.Items.Clear();
            Query mquery = new Query();
            string sql;
            sql = string.Format(@"Select * from riji Where RJ_DATE>'{0}'  and RJ_YHXM like '%%{1}%%' and RJ_IP like '%%{2}%%' ", dateTimePicker1.Value.ToShortDateString() + " 00:00:00", text_user.Text, text_ip.Text);

            sql = sql + " order by RJ_DATE desc ";

            {
                DataTable dt = mquery.GetTable(sql);

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                        lt.SubItems.Add(dt.Rows[i]["RJ_YHXM"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["RJ_DATE"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["RJ_neirong"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["RJ_IP"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["RJ_CZLX"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["RJ_CZJG"].ToString().Trim());
                        var sneirong = "";
                        sneirong = DecodeBase64("utf-8", dt.Rows[i]["RJ_NRMS"].ToString().Trim());
                        int index = 0;
                        index = sneirong.IndexOf("here");
                        if (sneirong.Contains("here"))
                        {
                            lt.SubItems.Add(sneirong.Substring(index-1, sneirong.Length - index+1));
                        }
                        else
                        {
                            lt.SubItems.Add("");
                        }
                        lt.SubItems.Add(dt.Rows[i]["RJ_CZTJ"].ToString().Trim());

                        lt.SubItems.Add(dt.Rows[i]["RJ_GNKK"].ToString().Trim());
                        lt.SubItems.Add(sneirong);

                        this.lv.Items.Add(lt);
                    }
                }


            }



        }

        private void OperationLog_Load(object sender, EventArgs e)
        {
            ////判断是否民警
            //if (m_mainform.sfjy.Contains("检测过程查询"))
            //{
            //    if (!m_mainform.SFMJ.Contains("是"))
            //    {
            //        but_query.Enabled = false;
            //    }
            //}
            //if (!m_mainform.squanxian.Contains("操作日志查询"))
            //{
            //    but_query.Enabled = false;
            //}

            //if (!m_mainform.squanxian.Contains("操作日志统计"))
            //{
            //    but_tongji.Enabled = false;
            //}
            //if (!m_mainform.squanxian.Contains("操作日志导出"))
            //{
            //    but_daochu.Enabled = false;
            //}
            
        }

        private void but_tongji_Click(object sender, EventArgs e)
        {
            lv2.Visible = true;
            lv.Visible = false;
            tongji();
        }
        public void tongji()
        {
            lv2.Items.Clear();
            Query mquery = new Query();
            string sql;
            sql = string.Format(@" select RJ_YHXM from riji group by RJ_YHXM having count(RJ_YHXM)>0 ");//用户名
            DataTable dt = mquery.GetTable(sql);

            var name = "";
            var sdate = "";

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["RJ_YHXM"].ToString().Trim() + ":";
                   
                    sql = string.Format(@"select * from riji where RJ_YHXM='{0}' 
                                            and RJ_DATE>'{1}' ",
                                            dt.Rows[i]["RJ_YHXM"].ToString().Trim(), 
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate=mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                  
                }
            }

            string[] cname_yhxm = name.Split(new char[] { ':' });
            string[] cdate_yhxm = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";

            sql = string.Format(@" select RJ_IP from riji group by RJ_IP having count(RJ_IP)>0 ");//事件
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["RJ_IP"].ToString().Trim() + ":";
                   
                    sql = string.Format(@"select * from riji where RJ_IP='{0}' 
                                            and RJ_DATE>'{1}' ",
                                             dt.Rows[i]["RJ_IP"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_ip = name.Split(new char[] { ':' });
            string[] cdate_ip = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";

            sql = string.Format(@" select RJ_CZLX from riji group by RJ_CZLX having count(RJ_CZLX)>0 ");//
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["RJ_CZLX"].ToString().Trim() + ":";
                 
                    sql = string.Format(@"select * from riji where RJ_CZLX='{0}' 
                                            and RJ_DATE>'{1}' ",
                                            dt.Rows[i]["RJ_CZLX"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_czlx = name.Split(new char[] { ':' });
            string[] cdate_czlx = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";

            sql = string.Format(@" select RJ_CZJG from riji group by RJ_CZJG having count(RJ_CZJG)>0 ");//
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["RJ_CZJG"].ToString().Trim() + ":";
                   
                    sql = string.Format(@"select * from riji where RJ_CZJG='{0}' 
                                            and RJ_DATE>'{1}' ",
                                            dt.Rows[i]["RJ_CZJG"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_czjg= name.Split(new char[] { ':' });
            string[] cdate_czjg = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";

            sql = string.Format(@" select RJ_GNKK from riji group by RJ_GNKK having count(RJ_GNKK)>0 ");//
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["RJ_GNKK"].ToString().Trim() + ":";
                  
                    sql = string.Format(@"select * from riji where RJ_GNKK='{0}' 
                                            and RJ_DATE>'{1}' ",
                                            dt.Rows[i]["RJ_GNKK"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_gnkk = name.Split(new char[] { ':' });
            string[] cdate_gnkk = sdate.Split(new char[] { ':' });


            var count = 0;
            if (count < cname_yhxm.Length)
            {
                count = cname_yhxm.Length;
            }
            if (count < cname_ip.Length)
            {
                count = cname_ip.Length;
            }
            if (count < cname_czlx.Length)
            {
                count = cname_czlx.Length;
            }
            if (count < cname_czjg.Length)
            {
                count = cname_czjg.Length;
            }
            if (count < cname_gnkk.Length)
            {
                count = cname_gnkk.Length;
            }
            
            count = count - 1;
            for (int i = 0; i < count; i++)
            {
                ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                if (i < cname_yhxm.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_yhxm[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_yhxm[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }
                //2
                if (i < cname_ip.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_ip[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_ip[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }
                //3
                if (i < cname_czlx.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_czlx[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_czlx[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }

                //4
                if (i < cname_czjg.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_czjg[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_czjg[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }

                //5
                if (i < cname_gnkk.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_gnkk[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_gnkk[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }

               

                this.lv2.Items.Add(lt);

            }
            

        }

        private void but_daochu_Click(object sender, EventArgs e)
        {
            lv2.Visible = true;
            lv.Visible = false;
            tongji();

            string path = "";
            path = Application.StartupPath + path + DateTime.Now.ToString("yyyyMMdd") + ".xls";
            excel(path);
        }

        private void excel(string path)
        {
            try
            {
                StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
                StringBuilder sb = new StringBuilder();

                sb.Append("统计时间从" + dateTimePicker1.Value.ToShortDateString() + "至" + DateTime.Now.ToString("yyyy-MM-dd") + "\t");
                sb.Append(Environment.NewLine);
                for (int i = 0; i < lv2.Columns.Count; i++)
                    sb.Append(lv2.Columns[i].Text.ToString() + "\t");
                sb.Append(Environment.NewLine);

                foreach (ListViewItem li in lv2.Items)
                {
                    for (int i = 0; i < lv2.Columns.Count; i++)
                        sb.Append(li.SubItems[i].Text + "\t");
                    sb.Append(Environment.NewLine);
                }
                sw.Write(sb.ToString());
                sw.Flush();
                sw.Close();
                MessageBox.Show("成功保存于" + path + "文件！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
